package com.zt.student.shiro;

import com.zt.common.pojo.origin.OriginStudent;
import com.zt.student.service.OriginStudentService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * description:用于微信登录后绑定originStudent时校验
 * author:zt
 * date:2022-01-04
 */
public class PwdRealm extends AuthorizingRealm {

    @Autowired
    private OriginStudentService originStudentService;

    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof UsernamePasswordToken;
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        String studentId = (String) token.getPrincipal();
        OriginStudent studentDB = originStudentService.getOriginStuById(studentId);

        if (studentDB == null){
            throw new UnknownAccountException();
        }

        //密码加盐处理
        return new SimpleAuthenticationInfo(studentDB,studentDB.getPassword(), ByteSource.Util.bytes(studentDB.getSalt()),getName());
    }
}
